﻿using UnityEditor;
using UnityEngine;

using System.Collections;
using System.Collections.Generic;

using System.IO;
using g.txt;

class DebugWindow : EditorWindow
{
    public enum EIdleType
    {
        NORMAL,
        IDLE2,
        IDLE3,
        IDLE4,
    }
    private int mDeltaStamina;

    private bool duel;

    private bool showCollision;
    private bool showTutorial;
    private bool mNoEnemy;
    private bool showGrid;
    private bool mMaxLevel;

    private float timeModifyValue;
    private int timeTypeIndex;
    private bool mEnemyFreeze;
    private bool unlimitedTime;
    private bool mSkillNoCd;
    private bool mBattleLog;
    private string mSaveDataStr;
    private Vector2 mScrollPosition;
    private EIdleType mIdleType;
    private int mReplayCode;
    private string mSkillLevel = "";
    private static string[] timeTypeName = 
    {
        "秒",
        "分",
        "小时",
        "天",
    };

    [MenuItem("FreyrTools/Debug/DebugWindow")]
    static void Init()
    {
        EditorWindow.GetWindow(typeof(DebugWindow)).Show();
    }

    void OnEnable()
    {
#if PROFILE_BEHAVIOUR || PROFILE_ALL
		SwProfiler.GetInstance().BeginSample("DebugWindow.OnEnable()", "DebugWindow.OnEnable");
#endif
        unlimitedTime = PlayerPrefs.GetInt("UNLIMITED_TIME") == 1;
        mEnemyFreeze = PlayerPrefs.GetInt("mEnemyFreeze") == 1;
        mNoEnemy = PlayerPrefs.GetInt("不出怪") == 1;
        mSkillNoCd = PlayerPrefs.GetInt("无限技能") == 1;
        showTutorial = PlayerPrefs.GetInt("显示教程") == 1;
        showGrid = PlayerPrefs.GetInt("显示格子") == 1;
        mMaxLevel = PlayerPrefs.GetInt("技能改造") == 1;
        mIdleType = (EIdleType)PlayerPrefs.GetInt("IdleType");
#if PROFILE_BEHAVIOUR || PROFILE_ALL
		SwProfiler.GetInstance().EndSample("DebugWindow.OnEnable");
#endif
    }

    void OnGUI()
    {
#if PROFILE_BEHAVIOUR || PROFILE_ALL
		SwProfiler.GetInstance().BeginSample("DebugWindow.OnGUI()", "DebugWindow.OnGUI");
#endif
        mScrollPosition = GUILayout.BeginScrollView(mScrollPosition, false, true, GUILayout.Width(500));

        GUILayout.BeginHorizontal();
        GUILayout.Label("体力值:", GUILayout.Width(50f));
        mDeltaStamina = EditorGUILayout.IntField(mDeltaStamina, GUILayout.Width(50f));

        if (GUILayout.Button("增加", GUILayout.Width(100f)))
        {
            GameApp.GetInstance().GetUserState().AddStamina(mDeltaStamina);
            GameApp.GetInstance().Save();
        }

        if (GUILayout.Button("消耗", GUILayout.Width(100f)))
        {
            if (GameApp.GetInstance().GetUserState().UseStamina(mDeltaStamina))
            {
                GameApp.GetInstance().Save();
            }
            else
            {
                UIMsgBox.instance.ShowConfirmMessage("体力不够，请充值！");
            }
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        mSaveDataStr = PlayerPrefs.GetString("存档ID");
        GUILayout.Label("存档ID : ", GUILayout.Width(100));
        string newSaveDataStr = GUILayout.TextField(mSaveDataStr, GUILayout.Width(100f));
        if (mSaveDataStr != newSaveDataStr)
        {
            mSaveDataStr = newSaveDataStr;
            PlayerPrefs.SetString("存档ID", mSaveDataStr);
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (duel != GUILayout.Toggle(duel, "与robot对决", GUILayout.Width(100)))
        {
            duel = !duel;
            PlayerPrefs.SetInt("与robot对决", duel == true ? 1 : 0);

            if (duel)
            {
                PlayerPrefs.SetInt("不出怪", 1);
            }
            else
            {
                PlayerPrefs.SetInt("不出怪", 0);
            }
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (showCollision != GUILayout.Toggle(showCollision, "显示碰撞", GUILayout.Width(100)))
        {
            showCollision = !showCollision;
            PlayerPrefs.SetInt("显示碰撞", showCollision == true ? 1 : 0);

            GameObject collisionRoot = GameObject.Find("_____COLLISION");
            if (collisionRoot != null && collisionRoot.transform.childCount > 0)
            {
                GameObject collision = collisionRoot.transform.GetChild(0).gameObject;
                for (int i = 0; i < collision.transform.childCount; i++)
                {
                    GameObject c = collision.transform.GetChild(i).gameObject;
                    if (!c.name.Contains("Ground"))
                    {
                        Renderer renderer = c.GetComponent<Renderer>();
                        if (renderer != null)
                        {
                            renderer.enabled = showCollision;
                        }
                    }
                }
            }
        }

        if (showTutorial != GUILayout.Toggle(showTutorial, "显示教程", GUILayout.Width(100)))
        {
            showTutorial = !showTutorial;
            PlayerPrefs.SetInt("显示教程", showTutorial ? 1 : 0);
        }

        //if (mMaxLevel != GUILayout.Toggle(mMaxLevel, "技能改造", GUILayout.Width(100)))
        //{
        //    mMaxLevel = !mMaxLevel;
        //    PlayerPrefs.SetInt("技能改造", mMaxLevel ? 1 : 0);
        //}
        GUILayout.Label("技能等级: ", GUILayout.Width(80));
        mSkillLevel = PlayerPrefs.GetString("技能等级");
        string tempString = EditorGUILayout.TextField(mSkillLevel);
        if (tempString != mSkillLevel)
        {
            mSkillLevel = tempString;
            PlayerPrefs.SetString("技能等级", mSkillLevel);
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("所有道具+20个", GUILayout.Width(200)))
        {
            Dictionary<short, Sw2AItem> itemDic = GameApp.GetInstance().GetUserState().GetItemDic();
            foreach (Sw2AItem item in itemDic.Values)
            {
                item.ItemCount += 20;
            }
            if (TempPackageUIScript.mInstance != null)
            {
                TempPackageUIScript.mInstance.CreateItems(TempPackageUIScript.EPackageFilterType.ALL);
            }
        }
        if (GUILayout.Button("更新动画长度", GUILayout.Width(200)))
        {
            CreateAnimationExcel();
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("更新globalKey", GUILayout.Width(200)))
        {
            AssetDatabase.Refresh();
            GTXTManager.GetInstance().Init();
            Dictionary<short, TXTFactory.TXTglobal> globalDic = GTXTManager.GetInstance().GetData<GTXTStorageglobal>().GetDataDic();
            Dictionary<short, TXTFactory.TXTinitialization> initializationDic = GTXTManager.GetInstance().GetData<GTXTStorageinitialization>().GetDataDic();
            if (Application.platform == RuntimePlatform.WindowsEditor)
            {
                string path = Application.dataPath + "/Script/Game/GlobalKey.cs";
                try
                {
                    FileStream aFile = new FileStream(path, FileMode.Create);
                    StreamWriter sw = new StreamWriter(aFile);
                    sw.WriteLine("public class GlobalKey");
                    sw.WriteLine("{");
                    foreach (KeyValuePair<short, TXTFactory.TXTglobal> pair in globalDic)
                    {
                        sw.WriteLine("    public const short " + pair.Value.gname + " = " + pair.Key + ";");
                    }
                    sw.WriteLine("}");

                    sw.WriteLine();

                    sw.WriteLine("public class InitializationKey");
                    sw.WriteLine("{");
                    foreach (KeyValuePair<short, TXTFactory.TXTinitialization> pair in initializationDic)
                    {
                        sw.WriteLine("    public const short " + pair.Value.iname + " = " + pair.Key + ";");
                    }
                    sw.WriteLine("}");

                    sw.Close();
                    aFile.Close();
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                }
                catch
                {
#if PROFILE_STOP || PROFILE_ALL
				SwProfiler.GetInstance().StopProfile("DebugWindow.OnGUI()");
#endif
                    return;
                }

                string javaPath = Application.dataPath + "/../../../server/trunk/Sw2AGame/src/com/freyr/sw2/txt/storage/GlobalKey.java";
                try
                {
                    FileStream aFile = new FileStream(javaPath, FileMode.Create);
                    StreamWriter sw = new StreamWriter(aFile);
                    sw.WriteLine("package com.freyr.sw2.txt.storage;");
                    sw.WriteLine();
                    sw.WriteLine("public class GlobalKey");
                    sw.WriteLine("{");
                    foreach (KeyValuePair<short, TXTFactory.TXTglobal> pair in globalDic)
                    {
                        sw.WriteLine("    public static final short " + pair.Value.gname + " = " + pair.Key + ";");
                    }
                    sw.WriteLine("}");
                    sw.Close();
                    aFile.Close();
                }
                catch
                {
#if PROFILE_STOP || PROFILE_ALL
				SwProfiler.GetInstance().StopProfile("DebugWindow.OnGUI()");
#endif
                    return;
                }

                string javaTxtPath = Application.dataPath + "/../../../server/trunk/Sw2AGame/src/com/freyr/sw2/txt/storage/GlobalInfo.txt";
                try
                {
                    FileStream aFile = new FileStream(javaTxtPath, FileMode.Create);
                    StreamWriter sw = new StreamWriter(aFile);
                    sw.WriteLine("Copy To SwGlobal");
                    sw.WriteLine();
                    foreach (KeyValuePair<short, TXTFactory.TXTglobal> pair in globalDic)
                    {
                        string typeStr = "";
                        if (pair.Value.gtype == "" || pair.Value.gtype == null)
                        {
                            typeStr = "float";
                            continue;
                        }
                        else
                        {
                            typeStr = pair.Value.gtype;
                        }
                        sw.WriteLine();
                        sw.WriteLine("  /** " + pair.Value.gdesc + " */");
                        string typeEnd = "";
                        if (typeStr == "float")
                        {
                            typeEnd = "f";
                        }
                        sw.WriteLine("  public static " + typeStr + " " + pair.Value.gname + " = " + pair.Value.gvalue + typeEnd + ";");
                    }
                    sw.Close();
                    aFile.Close();
                }
                catch
                {
#if PROFILE_STOP || PROFILE_ALL
				SwProfiler.GetInstance().StopProfile("DebugWindow.OnGUI()");
#endif
                    return;
                }

                javaPath = Application.dataPath + "/../../../server/trunk/Sw2AGame/src/com/freyr/sw2/txt/storage/InitializationKey.java";
                try
                {
                    FileStream aFile = new FileStream(javaPath, FileMode.Create);
                    StreamWriter sw = new StreamWriter(aFile);
                    sw.WriteLine("package com.freyr.sw2.txt.storage;");
                    sw.WriteLine();
                    sw.WriteLine("public class InitializationKey");
                    sw.WriteLine("{");
                    foreach (KeyValuePair<short, TXTFactory.TXTinitialization> pair in initializationDic)
                    {
                        sw.WriteLine("    public static short " + pair.Value.iname + " = " + pair.Key + ";");
                    }
                    sw.WriteLine("}");
                    sw.Close();
                    aFile.Close();
                }
                catch
                {
#if PROFILE_STOP || PROFILE_ALL
				SwProfiler.GetInstance().StopProfile("DebugWindow.OnGUI()");
#endif
                    return;
                }
            }
        }
        if (GUILayout.Button("更新EffectPoolType", GUILayout.Width(200)))
        {
            GenerateEffectPoolType();
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        //if (GUILayout.Button("拷贝英雄动画", GUILayout.Width(200)))
        //{
        //    CopyCharacterAnimation();
        //}
        if (GUILayout.Button("显示prefab名字", GUILayout.Width(200)))
        {
            List<GameObject> gameObjects = new List<GameObject>(Selection.gameObjects);
            gameObjects.Sort((x, y) => x.name.CompareTo(y.name));
            string log = "";

            foreach (GameObject go in gameObjects)
            {
                log += AssetDatabase.GetAssetPath(go).Replace("Assets/Resources/SW3_Effect/", "").Replace(".prefab", "");
                log += "\t" + go.name.ToUpper();
                log += "\n";
            }
            Debug.Log(log);
        }
        if (GUILayout.Button("更新HeroPrefab", GUILayout.Width(200)))
        {
            UpdateCharacterPrefab();
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("更新特效配置", GUILayout.Width(200)))
        {
            UpdateEffectScript();
        }
        if (GUILayout.Button("显示Transform路径", GUILayout.Width(200)))
        {
            Transform go = Selection.activeTransform;
            Debug.Log(GetTransformPath(go));
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("删存档", GUILayout.Width(200)))
        {
            string path = Application.dataPath + "/../../Documents/"; ;
            try
            {
                DirectoryInfo document = new DirectoryInfo(path);
                document.Delete(true);
            }
            catch
            {
#if PROFILE_STOP || PROFILE_ALL
			SwProfiler.GetInstance().StopProfile("DebugWindow.OnGUI()");
#endif
                return;
            }
        }
        if (GUILayout.Button("显示替换正则表达式", GUILayout.Width(200)))
        {
            Debug.Log("#if.*\\|\\|\\sPROFILE_ALL\\r*\\n.*\\r*\\n#endif\\r*\\n");
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        //if (GUILayout.Button("切换自动", GUILayout.Width(200)))
        //{
        //    if (GameApp.GetInstance().GetGameWorld() != null)
        //    {
        //        if (GameApp.GetInstance().GetGameWorld().GetLocalPlayer() != null)
        //        {
        //            GameApp.GetInstance().GetGameWorld().GetLocalPlayer().CreateMindRobot(true);
        //        }
        //    }
        //}
        if (GUILayout.Button("我方加满能量", GUILayout.Width(150)))
        {
            if (GameApp.GetInstance().GetGameWorld() != null)
            {
                foreach (Hero robot in GameLogic.GetInstance().GetInPlayerStateEnteredHeroOfTeam(TeamName.Blue))
                {
                    robot.AddSkillEnergy(SwGlobal.SKILL_ENERGY_CONSUME, "", false);
                }
            }
        }
        if (GUILayout.Button("敌方加满能量", GUILayout.Width(150)))
        {
            if (GameApp.GetInstance().GetGameWorld() != null)
            {
                foreach (Hero robot in GameLogic.GetInstance().GetInPlayerStateEnteredHeroOfTeam(TeamName.Red))
                {
                    robot.AddSkillEnergy(SwGlobal.SKILL_ENERGY_CONSUME, "", false);
                }
            }
        }
        if (GUILayout.Button("加LoopRound数", GUILayout.Width(150)))
        {
            List<Hero> heros = GameLogic.GetInstance().GetLocalHeroList();
            foreach (Hero hero in heros)
            {
                hero.TryAddCurrentAttackRoundNum();
                hero.GetPlayerSkill().PlayerCheckRoundLoop();
            }
        }
        //if (GUILayout.Button("换人", GUILayout.Width(200)))
        //{
        //    if (GameApp.GetInstance().GetGameWorld() != null)
        //    {
        //        GameApp.GetInstance().GetGameWorld().ChangePlayerAndRobot();
        //    }
        //}
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("杀死所有怪物", GUILayout.Width(100f)))
        {
            if (GameWorldLinker.GetInstance() == null)
            {
                return;
            }
            GameLogic gameLogic = GameWorldLinker.GetInstance().GetGameLogic();
            if (null != gameLogic)
                gameLogic.OnMainMissionFinished();
        }
        if (GUILayout.Button("杀死自己", GUILayout.Width(100f)))
        {
            if (GameWorldLinker.GetInstance() == null)
            {
                return;
            }
            GameLogic gameLogic = GameWorldLinker.GetInstance().GetGameLogic();

            if (null != gameLogic)
            {
                if (gameLogic.GetInPlayingStateLocalHeroList().Count > 0)
                {
                    gameLogic.GetInPlayingStateLocalHeroList()[0].UnderAttack(new DamageProperty(null, (int)1e8));
                }
            }
        }
        if (GUILayout.Button("任务立即完成", GUILayout.Width(100f)))
        {
            PlayerPrefs.SetInt("任务立即完成", 1);
        }
        if (GUILayout.Button("任务立即失败", GUILayout.Width(100f)))
        {
            PlayerPrefs.SetInt("任务立即失败", 1);
        }        
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("我方眩晕", GUILayout.Width(100f)))
        {
            List<Hero> unitList = GameLogic.GetInstance().GetInPlayingStateLocalHeroList();
            foreach (Hero h in unitList)
            {
                List<object> mBuffParam = new List<object>();
                mBuffParam.Add(2f);
                mBuffParam.Add(0f);
                SkillBuff TriggeredBuff = SkillBuff.CreateBuff(SkillBuff.EBuffType.STUN, null, mBuffParam.ToArray());
                TriggeredBuff.VFXId = 202;
                h.TryTriggerSkillFromOtherPlayer(TriggeredBuff);
            }
        }
        EIdleType lastIdleType = mIdleType;
        mIdleType = (EIdleType)EditorGUILayout.EnumPopup(mIdleType);
        if (lastIdleType != mIdleType)
        {
            PlayerPrefs.SetInt("IdleType", (int)mIdleType);
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        Time.timeScale = EditorGUILayout.FloatField("TimeScle (很刺激): ", Time.timeScale, GUILayout.Width(200));
        GUILayout.Label(Sw2aTimeManager.Now.ToString());
        GUILayout.EndHorizontal();

        //GUILayout.BeginHorizontal();
        //GUILayout.Label(GameApp.GetInstance().GetUserState().GetTutorialStep() + " " + Sw2aTutorialManager.GetInstance().GetTutorialText());
        //GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        timeTypeIndex = EditorGUILayout.Popup(timeTypeIndex, timeTypeName);
        timeModifyValue = EditorGUILayout.FloatField(timeModifyValue);
        if (GUILayout.Button("改时间"))
        {
            if (Sw2aTimeManager.Instance != null)
            {
                Sw2aTimeManager.Instance.ModifyTime((byte)timeTypeIndex, timeModifyValue);
            }
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        float lastTimeScale = Sw2aTimeManager.DebugTimeScale;
        float newTimeScale = EditorGUILayout.FloatField("时间倍率: ", lastTimeScale);
        if (lastTimeScale != newTimeScale)
        {
            Sw2aTimeManager.Instance.SetServerTime(Sw2aTimeManager.Now.Ticks);
            Sw2aTimeManager.DebugTimeScale = newTimeScale;
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (mNoEnemy != GUILayout.Toggle(mNoEnemy, "不出怪", GUILayout.Width(100)))
        {
            mNoEnemy = !mNoEnemy;
            PlayerPrefs.SetInt("不出怪", mNoEnemy ? 1 : 0);

        }
        if (mEnemyFreeze != GUILayout.Toggle(mEnemyFreeze, "小怪不动", GUILayout.Width(100)))
        {
            mEnemyFreeze = !mEnemyFreeze;
            PlayerPrefs.SetInt("mEnemyFreeze", mEnemyFreeze ? 1 : 0);
        }
        if (showGrid != GUILayout.Toggle(showGrid, "显示格子", GUILayout.Width(100)))
        {
            showGrid = !showGrid;
            PlayerPrefs.SetInt("显示格子", showGrid ? 1 : 0);
            if (GameApp.GetInstance().GetGameWorld() != null)
            {
                GameApp.GetInstance().GetGameWorld().SetGridActive(showGrid);
            }
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (unlimitedTime != GUILayout.Toggle(unlimitedTime, "任务时间无限", GUILayout.Width(100f)))
        {
            unlimitedTime = !unlimitedTime;
            PlayerPrefs.SetInt("UNLIMITED_TIME", unlimitedTime ? 1 : 0);
        }
        if (mSkillNoCd != GUILayout.Toggle(mSkillNoCd, "无限技能", GUILayout.Width(100f)))
        {
            mSkillNoCd = !mSkillNoCd;
            PlayerPrefs.SetInt("无限技能", mSkillNoCd ? 1 : 0);
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        mBattleLog = PlayerPrefs.GetInt("BattleLog") == 1;
        if (mBattleLog != GUILayout.Toggle(mBattleLog, "BattleLog", GUILayout.Width(100f)))
        {
            mBattleLog = !mBattleLog;
            PlayerPrefs.SetInt("BattleLog", mBattleLog ? 1 : 0);
        }
        if (GUILayout.Button("End Battle Log"))
        {
            BattleLogManager.GetInstance().EndABattleLog();
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GameWorld.IsDirectResult != GUILayout.Toggle(GameWorld.IsDirectResult, "一帧见血", GUILayout.Width(100f)))
        {
            GameWorld.IsDirectResult = !GameWorld.IsDirectResult;
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        bool showLocDebug = PlayerPrefs.GetInt("显示已加loc") == 1;
        bool nowShowLocDebug = GUILayout.Toggle(showLocDebug, "显示已加 localization", GUILayout.Width(100f));
        if (nowShowLocDebug != showLocDebug)
        {
            if(nowShowLocDebug)
            {
                PlayerPrefs.SetInt("显示已加loc", 1);
            }
            else
            {
                PlayerPrefs.SetInt("显示已加loc", 0);
            }

        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        ForecastManager.OpenEffect = GUILayout.Toggle(ForecastManager.OpenEffect, "打开预测死亡", GUILayout.Width(100f));
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        SwGlobal.DEAD_SLOW_EFFECT_TIME = EditorGUILayout.FloatField("死亡后减速的持续时间 : ", SwGlobal.DEAD_SLOW_EFFECT_TIME, GUILayout.Width(200));
        SwGlobal.DEAD_SLOW_EFFECT_TIME_SCALE = EditorGUILayout.FloatField("死亡后减速的比例 : ", SwGlobal.DEAD_SLOW_EFFECT_TIME_SCALE, GUILayout.Width(200));
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        SwGlobal.BEFORE_DEAD_SLOW_EFFECT_FRAME = EditorGUILayout.IntField("死亡前减速的提前帧数 : ", SwGlobal.BEFORE_DEAD_SLOW_EFFECT_FRAME, GUILayout.Width(200));
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        mReplayCode = EditorGUILayout.IntField(mReplayCode,GUILayout.Width(100f));
        if (GUILayout.Button("DownLoad replay"))
        {
            ReplayManager.GetInstance().DownloadReplay(mReplayCode);
        }
        GUILayout.EndHorizontal();

//         GUILayout.BeginHorizontal();
//         string loginIP = PlayerPrefs.GetString("SW2A 服务器ip");
//         GUILayout.Label("SW2A 服务器ip : ",GUILayout.Width(111));
//         loginIP = GUILayout.TextField(loginIP, GUILayout.Width(100));
//         PlayerPrefs.SetString("SW2A 服务器ip", loginIP);
//         GUILayout.EndHorizontal();

        GUILayout.EndScrollView();
#if PROFILE_BEHAVIOUR || PROFILE_ALL
		SwProfiler.GetInstance().EndSample("DebugWindow.OnGUI");
#endif
    }
    public void UpdateEffectScript()
    {
        AssetDatabase.Refresh();
        string path = "SW3_Effect/";
        GTXTManager.GetInstance().Init();
        List<TXTFactory.TXTeffectconfig> globalDic = GTXTManager.GetInstance().GetData<GTXTStorageeffectconfig>().GetVFXList();
        foreach (TXTFactory.TXTeffectconfig config in globalDic)
        {
            GameObject go = SwResources.Load(path + config.filename) as GameObject;
            if (go == null)
            {
                Debug.LogError(config.filename);
            }
            Transform transform = go.transform;
            if (config.transform != "")
            {
                transform = transform.FindChild(config.transform);
            }
            EffectScript script = null;
            switch (config.effecttype)
            {
                case 0:
                    {
                        script = FreyrUtils.AddComponent<EffectProjectileScript>(transform.gameObject);
                        break;
                    }
                case 1:
                    {
                        script = FreyrUtils.AddComponent<EffectParabolaScript>(transform.gameObject);
                        break;
                    }
                case 2:
                    {
                        script = FreyrUtils.AddComponent<EffectScaleScript>(transform.gameObject);
                        break;
                    }
                case 3:
                    {
                        script = FreyrUtils.AddComponent<LockRotationScript>(transform.gameObject);
                        break;
                    }
                case 4:
                    {
                        script = FreyrUtils.AddComponent<EffectLockPositionScript>(transform.gameObject);
                        break;
                    }
                case 5:
                    {
                        script = FreyrUtils.AddComponent<EffectScaleProjectileScript>(transform.gameObject);
                        break;
                    }
                case 6:
                    {
                        script = FreyrUtils.AddComponent<PlayerAddEnergyEffectScript>(transform.gameObject);
                        break;
                    }
                case 7:
                    {
                        script = FreyrUtils.AddComponent<EffectDisableScript>(transform.gameObject);
                        break;
                    }
                case 8:
                    {
                        script = FreyrUtils.AddComponent<PlayerAbsorbEnergyEffectScript>(transform.gameObject);
                        break;
                    }
                case 9:
                    {
                        script = FreyrUtils.AddComponent<EffectOffsetScript>(transform.gameObject);
                        break;
                    }
                case 10:
                    {
                        script = FreyrUtils.AddComponent<EffectUltSkillScript>(transform.gameObject);
                        break;
                    }
                case 11:
                    {
                        if (transform != null)
                        {
                            transform.tag = TagName.TRAIL;
                        }
                        else
                        {
                            Debug.LogError("Wrong path: " + go.name + "/" + config.transform);
                        }
                        break;
                    }
                case 12:
                    {
                        script = FreyrUtils.AddComponent<Delay>(transform.gameObject);
                        break;
                    }
                case 13:
                    {
                        script = FreyrUtils.AddComponent<EffectFaceCameraScript>(transform.gameObject);
                        break;
                    }
                case 14:
                    {
                        script = FreyrUtils.AddComponent<RepeatScript>(transform.gameObject);
                        break;
                    }
                case 15:
                    {
                        script = FreyrUtils.AddComponent<ScaleParticles>(transform.gameObject);
                        break;
                    }
                case 16:
                    {
                        script = FreyrUtils.AddComponent<EffectBurstScript>(transform.gameObject);
                        break;
                    }
                case 17:
                    {
                        script = FreyrUtils.AddComponent<EffectLengthenScript>(transform.gameObject);
                        break;
                    }
                case 18:
                    {
                        script = FreyrUtils.AddComponent<RepeatSpawnScript>(transform.gameObject);
                        break;
                    }
            }
            if (script != null)
            {
                script.SetConfig(config);
            }
            EditorUtility.SetDirty(go);
        }
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
    public void GenerateEffectPoolType()
    {
        if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            AssetDatabase.Refresh();
            string path = Application.dataPath + "/Script/Game/EffectPoolType.cs";
            try
            {
                FileStream aFile = new FileStream(path, FileMode.Create);
                StreamWriter sw = new StreamWriter(aFile);
                GTXTManager.GetInstance().Init();
                List<TXTFactory.TXTvfxname> globalDic = GTXTManager.GetInstance().GetData<GTXTStoragevfxname>().GetVFXList();
                sw.WriteLine("public enum EffectPoolType");
                sw.WriteLine("{");
                sw.WriteLine("    NONE,");
                foreach (TXTFactory.TXTvfxname data in globalDic)
                {
                    sw.WriteLine("    " + data.effectpooltype + ",");
                }
                sw.WriteLine("    NUM,");
                sw.WriteLine("}");
                sw.Close();
                aFile.Close();
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }
            catch
            {
#if PROFILE_STOP || PROFILE_ALL
				SwProfiler.GetInstance().StopProfile("DebugWindow.OnGUI()");
#endif
                return;
            }
        }
    }
    private void CreateAnimationExcel()
    {
#if UNITY_EDITOR && UNITY_STANDALONE_WIN || GYY_PC
        string path = Application.dataPath + "/../../../../designer/trunk/_DataTool/data/branch/default/AnimationLength.xlsx";
        string playerPrefabPath = Application.dataPath + "/Resources/Character/Hero/";
        string enemyPrefabPath = Application.dataPath + "/Resources/Enemy/L1/";
        string bossPrefabPath = Application.dataPath + "/Resources/Enemy/L1/Boss/";
        string summonPrefabPath = Application.dataPath + "/Resources/Summon/";
        string playerPrefabResourcePath = "Character/Hero/";
        string enemyPrefabResourcePath = "Enemy/L1/";
        string bossPrefabResourcePath = "Enemy/L1/Boss/";
        string summonPrefabResourcePath = "Summon/";

        FileInfo newFile = new FileInfo(path);
        if (newFile.Exists)
        {
            newFile.Delete();  // ensures we create a new workbook  
            newFile = new FileInfo(path);
        }
        using (OfficeOpenXml.ExcelPackage package = new OfficeOpenXml.ExcelPackage(newFile))
        {
            // add a new worksheet to the empty workbook  
            OfficeOpenXml.ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("AnimationLength");
            worksheet.Cells[1, 1].Value = "Type";
            worksheet.Cells[1, 2].Value = "Id";
            worksheet.Cells[1, 3].Value = "AnimationName";
            worksheet.Cells[1, 4].Value = "Length";

            int lineIndex = 2;

            DirectoryInfo sourceDir = new DirectoryInfo(playerPrefabPath);
            foreach (FileInfo file in sourceDir.GetFiles("hero*.prefab"))
            {
                GameObject go = SwResources.Load(playerPrefabResourcePath + file.Name.Replace(".prefab", "")) as GameObject;
                short id = short.Parse(go.name.Replace("hero", ""));
                Animation animation = go.GetComponent<Animation>();
                foreach (AnimationState clip in animation)
                {
                    worksheet.Cells[lineIndex, 1].Value = 0;
                    worksheet.Cells[lineIndex, 2].Value = id;
                    worksheet.Cells[lineIndex, 3].Value = clip.name;
                    worksheet.Cells[lineIndex, 4].Value = Mathf.Round(clip.length * SwGlobal.FramePerSecond);
                    lineIndex++;
                }
            }
            sourceDir = new DirectoryInfo(summonPrefabPath);
            foreach (FileInfo file in sourceDir.GetFiles("*.prefab"))
            {
                GameObject go = SwResources.Load(summonPrefabResourcePath + file.Name.Replace(".prefab", "")) as GameObject;
                short id = 0;
                if (go.name == "hero5_feishu")
                {
                    id = 5;
                }
                Animation animation = go.GetComponent<Animation>();
                foreach (AnimationState clip in animation)
                {
                    worksheet.Cells[lineIndex, 1].Value = 3;
                    worksheet.Cells[lineIndex, 2].Value = id;
                    worksheet.Cells[lineIndex, 3].Value = clip.name;
                    worksheet.Cells[lineIndex, 4].Value = Mathf.Round(clip.length * SwGlobal.FramePerSecond);
                    lineIndex++;
                }
            }
            sourceDir = new DirectoryInfo(enemyPrefabPath);
            foreach (FileInfo file in sourceDir.GetFiles("enemy*.prefab"))
            {
                if (file.Name.Contains("_500") || file.Name.Contains("_1000") || file.Name.Contains("_elite"))
                {
                    continue;
                }
                GameObject go = SwResources.Load(enemyPrefabResourcePath + file.Name.Replace(".prefab", "")) as GameObject;
                int id = int.Parse(go.name.Replace("enemy", ""));
                Animation animation = go.GetComponent<Animation>();
                foreach (AnimationState clip in animation)
                {
                    worksheet.Cells[lineIndex, 1].Value = 1;
                    worksheet.Cells[lineIndex, 2].Value = id;
                    worksheet.Cells[lineIndex, 3].Value = clip.name;
                    worksheet.Cells[lineIndex, 4].Value = Mathf.Round(clip.length * SwGlobal.FramePerSecond);
                    lineIndex++;
                }
            }
            sourceDir = new DirectoryInfo(bossPrefabPath);
            foreach (FileInfo file in sourceDir.GetFiles("boss*.prefab"))
            {
                GameObject go = SwResources.Load(bossPrefabResourcePath + file.Name.Replace(".prefab", "")) as GameObject;
                int id = int.Parse(go.name.Replace("boss", ""));
                Animation animation = go.GetComponent<Animation>();
                foreach (AnimationState clip in animation)
                {
                    worksheet.Cells[lineIndex, 1].Value = 2;
                    worksheet.Cells[lineIndex, 2].Value = id;
                    worksheet.Cells[lineIndex, 3].Value = clip.name;
                    worksheet.Cells[lineIndex, 4].Value = Mathf.Round(clip.length * SwGlobal.FramePerSecond);
                    lineIndex++;
                }
            }
            package.Save();
        }
#endif
    }
    private void CopyCharacterAnimation()
    {
        string source = Application.dataPath + "/../../../../art/trunk/ani/hero/";
        string destination = Application.dataPath + "/Art/Character/Hero/";
        string playerPrefabResourcePath = "Character/Hero/";
        string playerPrefabPath = Application.dataPath + "/Resources/" + playerPrefabResourcePath;
        List<string> ignorList = new List<string> { "hero26gun", "hero26melee", "hero2" };

        DirectoryInfo sourceDir = new DirectoryInfo(source);
        DirectoryInfo destDir = new DirectoryInfo(destination);
        foreach (DirectoryInfo directory in sourceDir.GetDirectories("hero*"))
        {
            if (ignorList.Contains(directory.Name))
                continue;

            string tempDes = Path.Combine(destination, directory.Name);
            if (!Directory.Exists(tempDes))
            {
                Directory.CreateDirectory(tempDes);
            }
            foreach (FileInfo fi in directory.GetFiles())
            {
                fi.CopyTo(Path.Combine(tempDes, fi.Name), true);
            }
        }
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
    private void UpdateCharacterPrefab()
    {
        GameObject go = Selection.activeGameObject;
        if (go == null || !go.name.Contains("hero")) //选中某prefab只更新这个prefab 未选全更新
        {
            //string source = Application.dataPath + "/../../../../art/trunk/ani/hero/";
            //string destination = Application.dataPath + "/Art/Character/Hero/";
            //string playerPrefabResourcePath = "Character/Hero/";
            //string playerPrefabPath = Application.dataPath + "/Resources/" + playerPrefabResourcePath;

            //DirectoryInfo sourceDir = new DirectoryInfo(source);
            //DirectoryInfo destDir = new DirectoryInfo(destination);

            //foreach (DirectoryInfo directory in destDir.GetDirectories("hero*"))
            //{
            //    string tempDes = Path.Combine(destination, directory.Name);
            //    tempDes = tempDes.Substring(tempDes.IndexOf("Assets"));
            //    string gameObjectName = Path.Combine(tempDes, directory.Name + ".FBX");
            //    if (directory.Name.Contains("new"))
            //    {
            //        gameObjectName = Path.Combine(tempDes, directory.Name.Substring(0, directory.Name.Length - 4) + ".FBX");
            //    }
            //    string prefabName = Path.Combine(playerPrefabResourcePath, directory.Name);
            //    string createPrefabPath = Path.Combine(playerPrefabPath.Substring(playerPrefabPath.IndexOf("Assets")), directory.Name + ".prefab");

            //    ModelImporter importer = AssetImporter.GetAtPath(gameObjectName) as ModelImporter;
            //    importer.isReadable = false;
            //    GameObject a = AssetDatabase.LoadAssetAtPath(gameObjectName, typeof(GameObject)) as GameObject;
            //    a.GetComponent<UnityEngine.Animation>().playAutomatically = false;
            //    a.GetComponent<UnityEngine.Animation>().animatePhysics = false;
            //    a.GetComponent<UnityEngine.Animation>().cullingType = AnimationCullingType.AlwaysAnimate;
            //    GameObject prefab = SwResources.Load(prefabName) as GameObject;
            //    if (prefab == null)
            //    {
            //        prefab = PrefabUtility.CreatePrefab(createPrefabPath, a);
            //    }
            //    AnimationClip[] clips = AnimationUtility.GetAnimationClips(a);
            //    AnimationUtility.SetAnimationClips(prefab.GetComponent<UnityEngine.Animation>(), clips);
            //    prefab.GetComponent<UnityEngine.Animation>().playAutomatically = false;
            //    prefab.GetComponent<UnityEngine.Animation>().animatePhysics = false;
            //    prefab.GetComponent<UnityEngine.Animation>().cullingType = AnimationCullingType.AlwaysAnimate;
            //    FreyrUtils.AddComponent<HighlightableObject>(prefab); //高光描边脚本
            //    HeroComponentScript script = FreyrUtils.AddComponent<HeroComponentScript>(prefab); //添加HeroComponentScript脚本
            //    string heroObjectName = prefab.name;
            //    if (prefab.name.Contains("new"))
            //    {
            //        heroObjectName = prefab.name.Substring(0, prefab.name.Length - 4);
            //    }
            //    if (prefab.transform.FindChild(heroObjectName) == null)
            //    {
            //        Debug.LogError(prefab.name + " miss hero object!");
            //        continue;
            //    }
            //    script.HeroObject = prefab.transform.FindChild(heroObjectName).gameObject;
            //    if (prefab.name == "hero26") //26号特殊处理  有hero26melee;
            //    {
            //        script.MeleeHeroObject = prefab.transform.FindChild("hero26melee").gameObject;
            //        script.MeleeHeroObject.SetActive(false);
            //        prefab.transform.FindChild("Bip003").gameObject.SetActive(false);
            //        prefab.transform.FindChild("hero026zhuangbei").gameObject.SetActive(false);
            //    }
            //    if (prefab.name == "hero27")
            //    {
            //        prefab.transform.FindChild("hero27_pijian").gameObject.SetActive(false);
            //        prefab.transform.FindChild("hero27_wuqi02").gameObject.SetActive(false);
            //    }
            //    script.GunFireList.Clear();
            //    int index = 1;
            //    while (true)
            //    {
            //        bool found = false;
            //        Transform[] childs = prefab.GetComponentsInChildren<Transform>(true);
            //        string gunfireName = "Point0" + index;
            //        if (index >= 10)
            //        {
            //            gunfireName = "Point" + index;
            //        }
            //        foreach (Transform tr in childs)
            //        {
            //            if (tr.name == gunfireName)
            //            {
            //                script.GunFireList.Add(tr.gameObject);
            //                found = true;
            //                index++;
            //                break;
            //            }
            //        }
            //        if (!found)
            //        {
            //            break;
            //        }
            //    }
            //    if (index == 1)
            //    {
            //        Debug.LogError("No GunPoint!" + prefab.name);
            //    }
            //    prefab.tag = TagName.PLAYER_ROOT;
            //    EditorUtility.SetDirty(prefab);
            //    //short heroId = short.Parse(prefab.name.Replace("hero", ""));
            //    //AddHeroBodyEffects(prefab, heroId);
            //}

            //AssetDatabase.SaveAssets();
            //AssetDatabase.Refresh();
        }
        else
        {
            string source = Application.dataPath + "/../../../../art/trunk/ani/hero/";
            string destination = Application.dataPath + "/Art/Character/Hero/";
            string playerPrefabResourcePath = "Character/Hero/";
            string playerPrefabPath = Application.dataPath + "/Resources/" + playerPrefabResourcePath;
            DirectoryInfo sourceDir = new DirectoryInfo(source);
            DirectoryInfo destDir = new DirectoryInfo(destination);

            DirectoryInfo directory = destDir.GetDirectories(go.name)[0];
            string tempDes = Path.Combine(destination, directory.Name);
            tempDes = tempDes.Substring(tempDes.IndexOf("Assets"));
            string gameObjectName = Path.Combine(tempDes, directory.Name + ".FBX");
            if (directory.Name.Contains("new"))
            {
                gameObjectName = Path.Combine(tempDes, directory.Name.Substring(0, directory.Name.Length - 4) + ".FBX");
            }
            string prefabName = Path.Combine(playerPrefabResourcePath, directory.Name);
            string createPrefabPath = Path.Combine(playerPrefabPath.Substring(playerPrefabPath.IndexOf("Assets")), directory.Name + ".prefab");
            string createBackupPrefabPath = Path.Combine(playerPrefabPath.Substring(playerPrefabPath.IndexOf("Assets")), directory.Name + "_backup.prefab");

            ModelImporter importer = AssetImporter.GetAtPath(gameObjectName) as ModelImporter;
            importer.isReadable = false;
            GameObject a = AssetDatabase.LoadAssetAtPath(gameObjectName, typeof(GameObject)) as GameObject;
            a.GetComponent<UnityEngine.Animation>().playAutomatically = false;
            a.GetComponent<UnityEngine.Animation>().animatePhysics = false;
            a.GetComponent<UnityEngine.Animation>().cullingType = AnimationCullingType.AlwaysAnimate;

            GameObject backupPrefab = PrefabUtility.CreatePrefab(createBackupPrefabPath, go);
            GameObject prefab = PrefabUtility.CreatePrefab(createPrefabPath, a);

            if (prefab.name == "hero5")
            {
                GameObject tempObject = GameObject.Instantiate(prefab);
                GameObject feishu = GameObject.Instantiate(Resources.Load("Summon/hero5_feishu")) as GameObject;
                feishu.name = feishu.name.Replace("(Clone)", "");
                feishu.transform.parent = tempObject.transform;
                feishu.SetActive(false);
                prefab = PrefabUtility.ReplacePrefab(tempObject, prefab);
                GameObject.DestroyImmediate(tempObject);
            }

            FreyrUtils.AddComponent<HighlightableObject>(prefab); //高光描边脚本
            HeroComponentScript script = FreyrUtils.AddComponent<HeroComponentScript>(prefab);
            string heroObjectName = prefab.name;
            if (prefab.name.Contains("new"))
            {
                heroObjectName = prefab.name.Substring(0, prefab.name.Length - 4);
            }
            if (prefab.transform.FindChild(heroObjectName) == null)
            {
                Debug.LogError(prefab.name + " miss hero object!");
                //return;
            }
            else
            {
                script.HeroObject = prefab.transform.FindChild(heroObjectName).gameObject;
            }
            if (prefab.name == "hero26")
            {
                script.MeleeHeroObject = prefab.transform.FindChild("hero26melee").gameObject;
                script.MeleeHeroObject.SetActive(false);
                prefab.transform.FindChild("Bip003").gameObject.SetActive(false);
                prefab.transform.FindChild("hero026zhuangbei").gameObject.SetActive(false);
            }
            if (prefab.name == "hero27")
            {
                prefab.transform.FindChild("hero27_pijian").gameObject.SetActive(false);
                prefab.transform.FindChild("hero27_wuqi02").gameObject.SetActive(false);
            }
            script.GunFireList.Clear();
            int index = 1;
            while (true)
            {
                bool found = false;
                Transform[] childs = prefab.GetComponentsInChildren<Transform>(true);
                string gunfireName = "Point0" + index;
                if (index >= 10)
                {
                    gunfireName = "Point" + index;
                }
                foreach (Transform tr in childs)
                {
                    if (tr.name == gunfireName)
                    {
                        script.GunFireList.Add(tr.gameObject);
                        found = true;
                        index++;
                        break;
                    }
                }
                if (!found)
                {
                    break;
                }
            }
            if (index == 1)
            {
                Debug.LogError("No GunPoint!" + prefab.name);
            }
            bool success = true;
            foreach (Cloth cloth in backupPrefab.GetComponentsInChildren<Cloth>(true)) //复制Cloth
            {
                Transform transform = prefab.transform.FindChild(GetTransformPath(cloth.transform));
                if (transform != null)
                {
                    Cloth newCloth = transform.gameObject.AddComponent<Cloth>();
                    EditorUtility.CopySerialized(cloth, newCloth);
                }
                else
                {
                    success = false;
                    Debug.LogError("Missing Path: " + GetTransformPath(cloth.transform));
                }
            }
            foreach (JiggleBone cloth in backupPrefab.GetComponentsInChildren<JiggleBone>(true)) //复制乳摇脚本
            {
                Transform transform = prefab.transform.FindChild(GetTransformPath(cloth.transform));
                if (transform != null)
                {
                    JiggleBone newCloth = transform.gameObject.AddComponent<JiggleBone>();
                    EditorUtility.CopySerialized(cloth, newCloth);
                    newCloth.CalculateForward();
                }
                else
                {
                    success = false;
                    Debug.LogError("Missing Path: " + GetTransformPath(cloth.transform));
                }
            }
            foreach (CapsuleCollider cloth in backupPrefab.GetComponentsInChildren<CapsuleCollider>(true)) //复制cloth碰撞
            {
                Transform transform = prefab.transform.FindChild(GetTransformPath(cloth.transform));
                if (transform != null)
                {
                    CapsuleCollider newCloth = transform.gameObject.AddComponent<CapsuleCollider>();
                    EditorUtility.CopySerialized(cloth, newCloth);
                }
                else
                {
                    success = false;
                    Debug.LogError("Missing Path: " + GetTransformPath(cloth.transform));
                }
            }
            foreach (Transform tr in backupPrefab.GetComponentsInChildren<Transform>(true)) //复制tag  -- heroObject
            {
                if (tr == tr.root)
                    continue;

                Transform transform = prefab.transform.FindChild(GetTransformPath(tr));
                if (transform != null)
                {
                    transform.tag = tr.tag;
                    transform.gameObject.layer = tr.gameObject.layer;
                }
            }
            foreach (SwUvAnimationScript swUvScript in backupPrefab.GetComponentsInChildren<SwUvAnimationScript>(true))
            {
                Transform transform = prefab.transform.FindChild(GetTransformPath(swUvScript.transform));
                if (transform != null)
                {
                    SwUvAnimationScript newUvScript = transform.gameObject.AddComponent<SwUvAnimationScript>();
                    EditorUtility.CopySerialized(swUvScript, newUvScript);
                }
            }
            prefab.tag = TagName.PLAYER_ROOT;
            EditorUtility.SetDirty(prefab);
            EditorUtility.SetDirty(backupPrefab);
            short heroId;
            if (prefab.name.Contains("new"))
            {
                heroId = short.Parse(prefab.name.Replace("hero", "").Replace("_new", ""));
            }
            else
            {
                heroId = short.Parse(prefab.name.Replace("hero", ""));
            }
            AddHeroBodyEffects(prefab, heroId); //添加人物身上特效
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            if (success)
            {
                string path = Path.Combine(Application.dataPath, createBackupPrefabPath.Substring(createBackupPrefabPath.IndexOf("Resources")));
                File.Delete(path);
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }
    public string GetTransformPath(Transform transfrom)
    {
        string path = transfrom.name;
        Transform root = transfrom.root;
        while (transfrom.parent != null && transfrom.parent != root)
        {
            transfrom = transfrom.parent;
            path = path.Insert(0, transfrom.name + "/");
        }
        return path;
    }
    public void AddHeroBodyEffects(GameObject go, short heroId)
    {
        GameObject rootObject = GameObject.Instantiate(go);
        GTXTManager.GetInstance().Init();
        List<TXTFactory.TXTbodyeffect> effectDic = GTXTManager.GetInstance().GetData<GTXTStoragebodyeffect>().GetDataList(ECombatUnitType.HERO, heroId);
        int effectSettingIndex = 0;
        foreach (TXTFactory.TXTbodyeffect data in effectDic)
        {
            if (rootObject.transform.FindChild(data.transform + "/" + data.effectname) != null)
            {
                GameObject.DestroyImmediate(rootObject.transform.FindChild(data.transform + "/" + data.effectname).gameObject, true);
            }
            GameObject prefab = Resources.Load("SW3_Effect/" + data.effectname) as GameObject;
            if (prefab == null)
            {
                Debug.LogError(data.effectname + " missed!");
            }
            GameObject effect = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
            Transform parent = rootObject.transform;
            if (data.transform != "")
            {
                parent = rootObject.transform.FindChild(data.transform);
            }
            if (parent == null)
            {
                Debug.LogError(data.transform + " " + data.effectname + " attach failed!");
                break;
            }
            if (heroId == SwGlobal.HERO_26)
            {
                effect.transform.parent = parent;
            }
            else
            {
                effect.transform.SetParent(parent, false);
            }

            //Add effect setting
            List<TXTFactory.TXTbodyeffectsetting> bodyEffectSettings = GTXTManager.GetInstance().GetData<GTXTStoragebodyeffectsetting>().GetDataList(data.effectname);
            if (bodyEffectSettings != null)
            {
                BodyEffectScript bodyEffectScript = FreyrUtils.AddComponent<BodyEffectScript>(rootObject);
                for (int i = 0; i < bodyEffectSettings.Count; i++)
                {
                    TXTFactory.TXTbodyeffectsetting txtSetting = bodyEffectSettings[i];
                    if (bodyEffectScript.AnimationInfoList == null)
                    {
                        bodyEffectScript.AnimationInfoList = new List<BodyEffectScript.BodyEffectInfo>();
                    }
                    bodyEffectScript.AnimationInfoList.Add(new BodyEffectScript.BodyEffectInfo() { BodyEffect = effect, AnimationName = txtSetting.animationname, Index = effectSettingIndex, EventType = txtSetting.eventtype });
                    effectSettingIndex++;
                }
            }

            EditorUtility.SetDirty(rootObject);
        }
        PrefabUtility.ReplacePrefab(rootObject, go);
        GameObject.DestroyImmediate(rootObject);
    }
    public void UpdateUI()
    {
        GameObject go = Selection.activeGameObject;
        foreach (UISprite sprite in go.GetComponentsInChildren<UISprite>(true))
        {
            if (sprite.atlas != null && sprite.atlas.name == "BattleHUDAtlas")
            {
                if (sprite.spriteName == "skill_small" || sprite.spriteName == "btn_item" || sprite.spriteName == "talkbk" || sprite.spriteName == "head")
                {
                    sprite.atlas = (Resources.Load("UI/Menu/Shared/Res/ShareUIAtlas") as GameObject).GetComponent<UIAtlas>();
                    sprite.spriteName = "Share_04";
                }
                else if (sprite.spriteName == "black100")
                {
                    sprite.atlas = (Resources.Load("UI/Menu/Shared/Res/ShareUIAtlas") as GameObject).GetComponent<UIAtlas>();
                    sprite.spriteName = "Share_20";
                }
                else if (sprite.spriteName == "talk_btn")
                {
                    sprite.atlas = (Resources.Load("UI/Menu/Shared/Res/ShareUIAtlas") as GameObject).GetComponent<UIAtlas>();
                    sprite.spriteName = "Share_42";
                }
                else if (sprite.spriteName == "small_empty")
                {
                    sprite.atlas = (Resources.Load("UI/Menu/Shared/Res/ShareUIAtlas") as GameObject).GetComponent<UIAtlas>();
                }
                else if (sprite.spriteName == "small_full")
                {
                    sprite.atlas = (Resources.Load("UI/Menu/Shared/Res/ShareUIAtlas") as GameObject).GetComponent<UIAtlas>();
                }
                else if (sprite.spriteName == "jump")
                {
                    sprite.atlas = (Resources.Load("UI/Menu/Shared/Res/ShareUIAtlas") as GameObject).GetComponent<UIAtlas>();
                    sprite.spriteName = "Share_54";
                }
            }
            else if (sprite.atlas == null)
            {
                Debug.LogError(GetTransformPath(sprite.transform));
            }
        }
    }
}

